# 模拟登录配置管理
 
from urllib.error import URLError,HTTPError  
import urllib.parse  
import urllib.request
import re
import json
import http.cookiejar

def getIpAddress():
    return "pzgl.yzhlgl.com"

def doLogin(username,password):
	
	ip = getIpAddress()
	if ip == None:
		print('获取ip失败')
		return

	posturl = "http://%s/Sys/Login/ValidateLogOn" % ip	
	postdata = urllib.parse.urlencode({'UserName': username, 'password': password}).encode('utf-8')
	headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"}  

	webCookie = http.cookiejar.CookieJar()  
	opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(webCookie))
	urllib.request.install_opener(opener)

	req = urllib.request.Request(posturl, postdata, headers)
	res = urllib.request.urlopen(req)

	if res.status == 200:
		data = res.read()
		strdata = str(data, encoding = "utf-8")
		jsondata = json.loads(strdata)
		print(jsondata["msg"])
		return jsondata["flag"]

	else:
		print(res.status,res.reason)
		return False

#doLogin("crh","crh2012")